import pageination from '../../components/pageination/pageination.vue'
export default {
	data() {
		return {
			//数据
	      total: 0, //总条数
	      page: 1, //当前页
	      size: 10, //每页显示条数
	      search: '',
        auditDetails: false,
        auditDetails2: false,
	      uploadInitiation: false,
	      uploadMetaphase: false,
	      uploadClosure: false,
	      projectDetailsModelShow: false,
	      project: {}, //项目信息
        auditObj: {}, //审核信息对象
	      initiationValidate: {}, //立项校验对象
	      metaphaseValidate: {}, //中期校验对象
	      closureValidate: {}, //结题校验对象
	      projectInitiation: {
	      	accountStatus:1//到账情况
	      }, //项目立项对象
	      projectMetaphase: {}, //项目中期对象
	      projectClosure: {}, //项目结题对象
	      projectList: [], //项目列表
			uploadNum: 0,
			FileUpload: [],
			curId: 0,
      uploadFile2:true,//是否上传附件//为true是为了刚打开时，虽然没有上传附件，但是提示不应该提示
			uploadFile:true,//是否上传附件//为true是为了刚打开时，虽然没有上传附件，但是提示不应该提示
			curIndex:-1,//选中下标
		}
	},
	components: {
		pageination
	},
	created() { //调用方法
		let that = this;
	},
	mounted() {
    let that = this;
    //校验名称重复
    $.validator.addMethod("checkNum", function(value, element, params) {
      let flg = false;
      //let re = /^\d{1,11}|(\d{1,11}\.\d{1})$/;
      let re = /^\d{1,11}\.*?\d{0,1}$/;
      flg = re.test(value);
      if (flg) {
        flg = value > 0 && value < 10000000000;
      }
      return this.optional(element) || flg;
    }, "输入范围0.1到10000000000以内！");

    that.initiationValidate = $("#uploadInitiationForm").validate({
      ignore: [],
      rules: {
        taskBookUrl: { // 任务书
          required: true,
          maxlength: 500
        },
        budgetAmount: { // 总金额
          required: true,
          checkNum:true
        },
        accountStatus: { // 到账情况
          required: true
        }
      },
      messages: {
        taskBookUrl: { // 任务书
          required: "请上传任务书！",
          maxlength: "任务书地址大于500"
        },
        budgetAmount: { // 总金额
          required: "请输入总金额！"
        },
        accountStatus: { // 到账情况
          required: "请选择到账情况！"
        }
      },
      /*errorElement: "em",
      errorPlacement: function(error, element) {
        error.appendTo(element.parents(".error-box").children(".error-msg"));
      }*/
    });

    that.metaphaseValidate = $("#uploadMetaphaseForm").validate({
      ignore: [],
      rules: {
        metaphaseReportUrl: { // 中期报告
          //required: true,
          maxlength: 500
        },
        budgetAmount: { // 总金额
          required: true,
          checkNum: true
        },
        practicalAmount: { // 已用金额
          required: true,
          checkNum: true
        }
      },
      messages: {
        metaphaseReportUrl: { // 中期报告
          //required: "请上传中期报告！",
          maxlength: "中期报告地址大于500"
        },
        budgetAmount: { // 总金额
          required: "请输入总金额！"
        },
        practicalAmount: { // 已用金额
          required: "请输入已用金额！"
        }
      },
      /*errorElement: "em",
      errorPlacement: function(error, element) {
        error.appendTo(element.parents(".error-box").children(".error-msg"));
      }*/
    });

    that.closureValidate = $("#uploadClosureForm").validate({
      ignore: [],
      rules: {
        closureMaterialUrl: { // 结题材料
          required: true,
          maxlength: 500
        },
        budgetAmount: { // 总金额
          required: true,
          checkNum: true
        },
        closurePracticalAmount: { // 已用金额
          required: true,
          checkNum: true
        }
      },
      messages: {
        closureMaterialUrl: { // 结题材料
          required: "请上传结题材料！",
          maxlength: "结题材料地址大于500"
        },
        budgetAmount: { // 总金额
          required: "请输入总金额！"
        },
        closurePracticalAmount: { // 已用金额
          required: "请输入已用金额！"
        }
      },
      /*errorElement: "em",
      errorPlacement: function(error, element) {
        error.appendTo(element.parents(".error-box").children(".error-msg"));
      }*/
    });
  },
	methods: { //写函数方法
		//给选中的列加背景色
		choosed:function(index){
			this.curIndex=index;
		},
		//页码切换执行方法
		pageFn: function(val, pid) {
			this.page = parseInt(val);
			//此处调列表数据接口
      this.searchTodo();
		},
		/**返回子组件用户选择的一页显示条数**/
		obtainChild: function(data) {
			this.row = data;
		},
		touchOffIdInput(n) {
			this.uploadNum = n;
			if(n == 1) {
				$("#taskBookFile").click();
			} else if (n == 2) {
        $("#metaphaseReportFile").click();
      } else if (n = 3) {
        $("#closureMaterialFile").click();
      }
		},
    /**文件上传js**/
    idFrontImg: function(event) {
      let that = this;
      var file = event.currentTarget.files[0];
      if (!that.fileGlobalFilter(file)) {
        return;
      }
      let formData = new FormData();
      formData.append('fileNames', file);
      let config = {
        headers: {
          'Content-Type': 'multipart/form-data;boundary = ' + new Date().getTime()
        }
      };
      that.$post(
        that.urls.upload,
        formData,
        config
      )
      .then(function(res) {
        if(res.success) {
          let fileInfos = res.data;
          let fileInfo = fileInfos[0];
          that.FileUpload = [that.getFileName(fileInfo.url)];
          that.projectInitiation.taskBookUrl = fileInfo.url;
          that.projectMetaphase.metaphaseReportUrl  = fileInfo.url;
          that.projectClosure.closureMaterialUrl  = fileInfo.url;

          /*$("input[name='taskBookUrl']").val(fileInfo.url);
          $("input[name='metaphaseReportUrl']").val(fileInfo.url);
          $("input[name='closureMaterialUrl']").val(fileInfo.url);
          that.initiationValidate.form();
          that.metaphaseValidate.form();
          that.closureValidate.form();*/
          that.uploadFile=true;
        }
      })
    },


    //显示上传立项模态框
    showUploadInitiation: function(project) {
    	console.log(project)
      //清空表单校验提示
      $("#uploadInitiationForm").validate().resetForm();
      //清空表单数据
      $("input[type='reset']").trigger("click");
      this.FileUpload = [];
      this.projectInitiation = {};
      this.projectInitiation.accountStatus = project.accountStatus == null ? 1 : project.accountStatus;
      this.projectInitiation.id = project.id;
      this.projectInitiation.budgetAmount = project.budgetAmount;
      this.projectInitiation.taskBookUrl = project.taskBookUrl;
      if (project.taskBookUrl != null && project.taskBookUrl != "") {
        this.FileUpload = [this.getFileName(project.taskBookUrl)];
      }
      this.uploadFile = true;
      this.uploadInitiation = true;
    },
    //显示上传中期模态框
    showUploadMetaphase: function(project) {
      //清空表单校验提示
      $("#uploadMetaphaseForm").validate().resetForm();
      //清空表单数据
      $("input[type='reset']").trigger("click");
      this.FileUpload = [];
      this.projectMetaphase = {};
      this.projectMetaphase.id = project.id;
      this.projectMetaphase.budgetAmount = project.budgetAmount;
      this.projectMetaphase.metaphaseReportUrl = project.metaphaseReportUrl;
      if (project.metaphaseReportUrl != null && project.metaphaseReportUrl != "") {
        this.FileUpload = [this.getFileName(project.metaphaseReportUrl)];
      }
      this.projectMetaphase.practicalAmount = project.practicalAmount;
      this.uploadMetaphase = true;
    },
    //显示上传结题模态框
    showUploadClosure: function(project) {
      //清空表单校验提示
      $("#uploadClosureForm").validate().resetForm();
      //清空表单数据
      $("input[type='reset']").trigger("click");
      this.FileUpload = [];
      this.projectClosure = {};
      this.projectClosure.id = project.id;
      this.projectClosure.budgetAmount = project.budgetAmount;
      this.projectClosure.closurePracticalAmount = project.closurePracticalAmount;
      if (project.closureMaterialUrl != "" && project.closureMaterialUrl != null) {
        this.projectClosure.closureMaterialUrl = project.closureMaterialUrl;
        this.FileUpload = [this.getFileName(project.closureMaterialUrl)];
      }
      this.uploadFile2 = true;
      this.uploadClosure = true;
    },
    //项目详情模态框
    projectDetails: function(projectId) {
      this.getProject(projectId);
      this.projectDetailsModelShow = true;
    },
    //审核详情模态框显示
    auditDetailsShow: function(msg, enclosureUrl) {
    	console.log(msg, enclosureUrl)
      this.auditObj = {};
      this.auditObj.msg = msg;
      this.auditObj.enclosureUrl = enclosureUrl;
      this.auditDetails = true;
    },
    //审核详情模态框显示
    auditDetailsShow2: function(msg, enclosureUrl, deadlineBeginTime, deadlineEndTime) {
      this.auditObj = {};
      this.auditObj.msg = msg;
      this.auditObj.enclosureUrl = enclosureUrl;
      this.auditObj.deadlineBeginTime = deadlineBeginTime;
      this.auditObj.deadlineEndTime = deadlineEndTime;
      this.auditDetails2 = true;
    },

		//关闭模态框
		closeModel: function() {
      this.uploadInitiation = false;
      this.uploadMetaphase = false;
      this.uploadClosure = false;
      this.projectDetailsModelShow = false;
      this.uploadFile=true;//为true是为了刚打开时，虽然没有上传附件，但是提示不应该提示
      this.auditDetails = false;
      this.auditDetails2 = false;
		},
    //审核状态
    auditStatus: function(status) {
      if (status != null) {
        switch (status) {
          case 0:
            return "暂存";
          case 1:
            return "待审核";
          case 2:
            return "审核通过";
          case 3:
            return "审核不通过";
          case 4:
            return "限期整改";
        }
      }
    },
    //预览
    preview: function(url,msg) {
      if (url == null || url == "") {
        if (msg == null || msg == "") {
          msg = "附件";
        }
        layer.msg("暂无" + msg + "！");
      } else {
        window.open(this.urls.baseUrl + url);
      }
    },
    //删除附件
    deleteEnclosure: function() {
      this.FileUpload = [];

      this.projectClosure.closureMaterialUrl = "";
      $("input[name='closureMaterialUrl']").val("");

      this.projectMetaphase.metaphaseReportUrl = "";
      $("input[name='metaphaseReportUrl']").val("");

      this.projectInitiation.taskBookUrl = "";
      $("input[name='taskBookUrl']").val("");
      this.uploadFile=false;
    },
    /*项目信息*/
    searchTodo: function () {
      let that = this;
      that.$post(
        that.urls.getCurrentExpertProjectPageInfo,
        {"pageNum": that.page,"pageSize": that.size,"search": that.search}
      )
      .then(function(res) {
        if(res.success) {
          that.projectList = res.pageInfo.list;
          that.total = res.pageInfo.total;
          that.page = res.pageInfo.pageNum;
        }
      })
    },
    //获取项目
    getProject: function(id) {
      let that = this;
      that.project = {};
      if (id != null) {
        that.$post(
          that.urls.getProjectInfo, {
            "id": id
          }
        )
        .then(function(res) {
          if(res.success) {
            that.project = res.data;
          }
        })
      }
    },
    //保存项目立项信息
    saveProjectInitiation: function () {
      let that = this;
      if(this.FileUpload.length){
      	this.uploadFile=true;
      }else{
      	this.uploadFile=false;
      }
      /*if (that.projectInitiation.taskBookUrl == null || that.projectInitiation.taskBookUrl == '') {
        return;
      }*/
      let flg = that.initiationValidate.form();
      if (that.projectInitiation.taskBookUrl == null || that.projectInitiation.taskBookUrl == '') {
        return;
      }
      if (flg) {
        that.$post(
          that.urls.addProjectInitiation,
          that.projectInitiation
        )
        .then(function(res) {
          let msg = "";
          if(res.success) {
            msg = "成功上传任务书！";
            that.searchTodo();
            that.closeModel();
          } else {
            msg = "上传失败！";
          }
          layer.msg(msg, {
            time: 3000, //3s后自动关闭
          });
        })
      }
    },
    //保存项目中期信息
    saveProjectMetaphase: function () {
      let that = this;
      let flg = that.metaphaseValidate.form();
      if (flg) {
        that.$post(
          that.urls.addProjectMetaphase,
          that.projectMetaphase
        )
        .then(function(res) {
          let msg = "";
          if(res.success) {
            msg = "成功上传中期报告！";
            that.searchTodo();
            that.closeModel();
          } else {
            msg = "上传失败！";
          }
          layer.msg(msg, {
            time: 3000, //3s后自动关闭
          });
        })
      }
    },
    //保存项目结题信息
    saveProjectClosure: function () {
      let that = this;
      if(this.FileUpload.length){
        this.uploadFile2=true;
      }else{
        this.uploadFile2=false;
      }
      /*if (that.projectClosure.closureMaterialUrl == null || that.projectClosure.closureMaterialUrl == '') {
        return;
      }*/
      let flg = that.closureValidate.form();
      //因为测试要求显示所有提示，因此改在了下面
      if (that.projectClosure.closureMaterialUrl == null || that.projectClosure.closureMaterialUrl == '') {
        return;
      }
      if (flg) {
        that.$post(
          that.urls.addProjectClosure,
          that.projectClosure
        )
        .then(function(res) {
          let msg = "";
          if(res.success) {
            msg = "成功上传结题报告！";
            that.searchTodo();
            that.closeModel();
          } else {
            msg = "上传失败！";
          }
          layer.msg(msg, {
            time: 3000, //3s后自动关闭
          });
        })
      }
    },
    //下载模板
    downloadTemplate: function(type) {
      let that = this;
      //模板类型(0：其他;1:基地申请；2：项目申报；3：项目申请；4：合作合同；5：任务书；6：中期报告；7：结题报告)
      if (type == null) {
        return;
      }
      that.$post(
        that.urls.getTemplate, {
          "type": type
        }
      )
      .then(function(res) {
        if(res.success) {
          let template = res.data;
          if (template.url == null || template.url == '') {
            let msg = "";
            switch (template.type){
              case 1:
                msg = "请联系管理员上传【基地申请模板】";
                break;
              case 2:
                msg = "请联系管理员上传【项目申报模板】";
                break;
              case 3:
                msg = "请联系管理员上传【项目申请模板】";
                break;
              case 4:
                msg = "请联系管理员上传【合作合同模板】";
                break;
              case 5:
                msg = "请联系管理员上传【任务书模板】";
                break;
              case 6:
                msg = "请联系管理员上传【中期报告模板】";
                break;
              case 7:
                msg = "请联系管理员上传【结题报告模板】";
                break;
            }
            layer.msg(msg, {
              time: 3000, //3s后自动关闭
            });
            return;
          }
          window.location.href = that.urls.download + "?path=" + template.url;
        }
      })
    },
	}
}
